package com.xiongda.controller;

import com.xiongda.common.Const;
import com.xiongda.common.ServerResponse;
import com.xiongda.pojo.BlogWithBLOBs;
import com.xiongda.pojo.User;
import com.xiongda.service.BlogService;
import com.xiongda.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 博客模块
 *
 * @author 缪琼
 */
@RestController
@RequestMapping(value = "/blog")
public class BlogController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BlogController.class);

    @Autowired
    private BlogService blogService;
    @Autowired
    private UserService userService;

    /**
     * 获取博客内容
     * @param id
     * @param session
     * @return
     */
    @RequestMapping(value = "/getblog", method = RequestMethod.GET)
    public ServerResponse getBlog(Long id, HttpSession session) {
        return blogService.getBlog(id);
    }

    /**
     * 获取该用户底下所有博客
     * @param userId
     * @param session
     * @return
     */
    @RequestMapping(value = "/getallblog", method = RequestMethod.GET)
    public ServerResponse getBlogByUser(@RequestParam(value = "user_id") Long userId,
                                        @RequestParam(value = "page_no", required = false, defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "page_size", required = false, defaultValue = "10") Integer pageSize,
                                        HttpSession session) {
        User user = userService.getUser(userId);
        if (user == null)
            return ServerResponse.createByErrorMessage("用户不存在");
        user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorMessage("参数错误");
        if (!userId.equals(user.getId()))
            return ServerResponse.createByErrorMessage("你没有权限进行该操作");
        return blogService.getBlogByUser(userId, pageNo, pageSize);
    }

    /**
     * 新增一篇博客或者修改博客
     * @return
     */
    @RequestMapping(value = "/saveoreditblog", method = RequestMethod.POST)
    public ServerResponse saveOrEditblog(@RequestBody BlogWithBLOBs blog, HttpSession session) {
        User user = userService.getUser(blog.getUserId());
        if (user == null)
            return ServerResponse.createByErrorMessage("用户不存在");
        user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorMessage("参数错误");
        if (!blog.getUserId().equals(user.getId()))
            return ServerResponse.createByErrorMessage("你没有权限进行该操作");
        ServerResponse response = blogService.saveAndEditBlog(blog);
        return !response.isSuccess() ? response : blog.getId() == null ? ServerResponse.createBySuccessMessage("新增成功") : ServerResponse.createBySuccessMessage("修改成功");
    }
}
